<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${pkName}.mapper.${className}Mapper">
    <sql id="baseQuery">
        SELECT
    <#list filedNames as f >${f.columnName}<#if f_has_next>,</#if> </#list>
        FROM  ${tableName}
    </sql>

    <!--说明：根据主键字段进行查询， 查询条件使用等号-->
    <select id="selectById" resultType="${pkName}.entity.${className}">
        <include refid="baseQuery"></include>
        where id =${r'#{id}'}
    </select>
    <!--说明：查询全部结果-->
    <select id="selectAll" resultType="${pkName}.entity.${className}">
        <include refid="baseQuery"></include>
    </select>

    <!--保存一个实体，null的属性也会保存，不会使用数据库默认值-->
    <insert id="insert">
        INSERT INTO
    ${tableName}
        (<#list filedNames as f >${f.columnName}<#if f_has_next>,</#if></#list>)
        values
        (<#list filedNames as f > ${r'#{'}${f.fiName}${r'}'}<#if f_has_next>,</#if></#list>)
    </insert>
    <!--说明：根据主键更新实体全部字段，null值会被更新-->
    <update id="updateById">
        UPDATE  ${tableName}
        SET
    <#list filedNames as f > ${f.columnName} = ${r'#{'}${f.fiName}${r'}'}<#if f_has_next>,</#if></#list>
        WHERE id=${r'#{id}'}
    </update>

    <!-- 说明：根据主键字段进行删除， 方法参数必须包含完整的主键属性-->
    <delete id="deleteById">
        DELETE FROM
    ${tableName}
        WHERE id =${r'#{id}'}
    </delete>

    <!--条件查询,符串类型模糊匹配-->
    <select id="selectByCondition" resultType="${pkName}.entity.${className}">
        <include refid="baseQuery"></include>
        <where>
        <#list filedNames as f >
            <if test="${f.fiName}!=null and  ${f.fiName}!='' ">
                <#if f.fiType = 'String'>
                    and  ${f.columnName} like concat('%',${r'#{'}${f.fiName}${r'}'},'%')
                <#else>
                    and  ${f.columnName}=${r'#{'}${f.fiName}${r'}'}
                </#if>
            </if>
        </#list>

        </where>
        order by created_time desc
    </select>


    <!--根据 id查询-->
    <select id="selectByIds" resultType="${pkName}.entity.${className}">
        <include refid="baseQuery"></include>
        <where>
            id in
            <foreach close=")" collection="array" index="index" item="id" open="(" separator=",">
            ${r'#{id}'}
            </foreach>
        </where>
    </select>


    <delete id="deleteByIds">
        DELETE FROM
       ${tableName}
        <where>
            id in
            <foreach close=")" collection="array" index="index" item="id" open="(" separator=",">
            ${r'#{id}'}
            </foreach>
        </where>
    </delete>


</mapper>